﻿using DICOM_PROJECT.DAL;
using DICOM_PROJECT.DAL.Security;
using DICOM_PROJECT.Extension;
using DICOM_PROJECT.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace DICOM_PROJECT.Controllers
{
    [CustomAuthorize(Roles="Patient")]
    public class PatientController : BaseController
    {
        //
        // GET: /Patient/
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public string GetPatientInfo()//(string id)
        {
            var user = db.UserProfiles.First(m => m.UserProfileID == User.UserID);
            var userResult = new List<PatientModel>
            {
                new PatientModel { ID=user.UserProfileID, SocialID=user.SocialID, Name = user.Name, Birthday=user.Birthday, Male=user.Male, Address = user.Address, Phone = user.Phone }
            };


            PageModel visitPage = new PageModel { RowsPerPage = 10, PageNumber = 1 };
            var visits = Pagination<Visit>.Page(user.Visits.OrderByDescending(v => v.VisitID), visitPage);
            var visitResult = new List<VisitModel>();
            visits.ForEach(v => visitResult.Add(new VisitModel { VisitID = v.VisitID, CreateDate = v.CreateDate, UpdateDate = v.UpdateDate, Symptom = v.Symptom }));

            var result = new ArrayList();
            result.Add(userResult);
            result.Add(visitResult);
            result.Add(visitPage);

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(result);
        }

        [HttpPost]
        public string GetVisit(string id)
        {
            int visitID = Int32.Parse(id);
            var visit = db.Visits.Find(visitID);
            var result = new ArrayList();
            result.Add(new VisitUpdateModel
            {
                VisitID = visit.VisitID,
                PatientID = visit.UserProfileID,
                PatientName = visit.UserProfile.Name,
                CreateDoctorID = visit.CreateDoctorID,
                CreateDoctorName = visit.CreateDoctor != null ? visit.CreateDoctor.Name : null,
                CreateDate = visit.CreateDate.ToString("HH:mm:ss dd/MM/yyyy"),
                UpdateDoctorID = visit.UpdateDoctorID,
                UpdateDoctorName = visit.UpdateDoctor != null ? visit.UpdateDoctor.Name : null,
                UpdateDate = visit.UpdateDate != null ? ((DateTime)visit.UpdateDate).ToString("HH:mm:ss dd/MM/yyyy") : null,
                Symptom = visit.Symptom,
                Diagnosis = visit.Diagnosis,
                Treatment = visit.Treatment
            });
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(result);
        }

        [HttpPost]
        public string GetVisitsPage(PageModel page)
        {
            var patient = new UserProfile();
            patient = db.UserProfiles.FirstOrDefault(m => m.UserProfileID == page.PatientID);
            PageModel visitPage = new PageModel { RowsPerPage = page.RowsPerPage, PageNumber = page.PageNumber };
            var visits = Pagination<Visit>.Page(patient.Visits.OrderByDescending(v => v.VisitID), visitPage);
            var visitResult = new List<VisitModel>();
            visits.ForEach(v => visitResult.Add(new VisitModel { VisitID = v.VisitID, CreateDate = v.CreateDate, UpdateDate = v.UpdateDate, Symptom = v.Symptom }));
            var result = new ArrayList();
            result.Add(visitResult);
            result.Add(visitPage);

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(result);
        }

        [HttpPost]
        public string SavePassword(ChangePasswordModel change)
        {
            List<string> result = new List<string>();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            var user = db.UserProfiles.First(m => m.UserProfileID == User.UserID);
            if(user.Password != change.CurrPass)
            {
                result.Add("invalid_password");
            }
            if(user.Password == change.NewPass && result.Count==0)
            {
                result.Add("same_password");
            }
            if(result.Count > 0)
            {
                result.Add("save_fail");
                return serializer.Serialize(result);
            }
            user.Password = change.NewPass;
            db.Entry(user).State = EntityState.Modified;
            db.SaveChanges();
            result.Add("save_success");
            return serializer.Serialize(result);
        }
    }
}